package com.cbts.action.person;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;



import com.cbts.entity.ExchangeRecord;
import com.cbts.entity.Gift;
import com.cbts.entity.Rule;
import com.cbts.entity.UserInfo;
import com.cbts.service.info.ExchangeManagementService;
import com.cbts.service.info.UserInfoService;
import com.cbts.service.person.ExchangeService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 * @author 邱鸿辉
 *
 */
public class ExchangeAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	private ExchangeService exchangeService;					//提供点数兑换服务
	private UserInfoService userInfoService;					//提供用户管理服务
	private ExchangeManagementService exchangeManagementService;//提供点数兑换管理服务
	private Gift gift;											//兑换物品
	private int count;											//要兑换的数量
	private Rule rule;											//点数兑换规则
	private UserInfo userInfo;									//员工
	private List<Gift> giftList;								//兑换物品列表
	private ExchangeRecord exchangeRecord;
	private List<ExchangeRecord> exchangeRecordList;
	private List<String> dateList = new ArrayList<String>();
	public ExchangeRecord getExchangeRecord() {
		return exchangeRecord;
	}
	public void setExchangeRecord(ExchangeRecord exchangeRecord) {
		this.exchangeRecord = exchangeRecord;
	}
	private String message;//
	public List<String> getDateList() {
		return dateList;
	}
	public void setDateList(List<String> dateList) {
		this.dateList = dateList;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public List<ExchangeRecord> getExchangeRecordList() {
		return exchangeRecordList;
	}
	public void setExchangeRecordList(List<ExchangeRecord> exchangeRecordList) {
		this.exchangeRecordList = exchangeRecordList;
	}
	/*public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}*/
	public List<Gift> getGiftList() {
		return giftList;
	}
	public void setGiftList(List<Gift> giftList) {
		this.giftList = giftList;
	}
	public UserInfo getUserInfo() {
		return userInfo;
	}
	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}
	public ExchangeManagementService getExchangeManagementService() {
		return exchangeManagementService;
	}
	public void setExchangeManagementService(
			ExchangeManagementService exchangeManagementService) {
		this.exchangeManagementService = exchangeManagementService;
	}

	public Rule getRule() {
		return rule;
	}
	public void setRule(Rule rule) {
		this.rule = rule;
	}
	public UserInfoService getUserInfoService() {
		return userInfoService;
	}
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
	public ExchangeService getExchangeService() {
		return exchangeService;
	}
	public void setExchangeService(ExchangeService exchangeService) {
		this.exchangeService = exchangeService;
	}
	public Gift getGift() {
		return gift;
	}
	public void setGift(Gift gift) {
		this.gift = gift;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	//进行兑换
	public String exchange()
	{
		try{
			
			userInfo = userInfoService.getUserInfo((String)ActionContext.getContext().getSession().get("userid"));
			//userInfo  = userInfoService.getUserInfo("1");
			/*System.out.println(gift.getGiftId().intValue());*/
			if("现金".equals(gift.getGiftName()))
			{
				if((gift=exchangeManagementService.getGiftByGiftName("现金"))==null)
				{
					message = "兑换失败";
					return ERROR;
				}
			}
			else
				gift = exchangeManagementService.getGift(gift.getGiftId().intValue());
			message = exchangeService.exchange(gift,count, userInfo);//调用service层的兑换服务
			this.setGift(null);
			if(!"兑换成功".equals(message))
			{
				return ERROR;
			}
			return SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
			message = "兑换失败";
			return ERROR;
		}
	}
	//获取进行兑换时页面
	public String getExchangePage()
	{
		if(message != null)
		{
			ActionContext.getContext().put("message", message);
			message = null;
		}
		userInfo = userInfoService.getUserInfo((String)ActionContext.getContext().getSession().get("userid"));
		exchangeRecordList = exchangeService.getExchangeRecordByUserid(userInfo.getUserid());
		for(int i=0;i<exchangeRecordList.size();i++)
			dateList.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(exchangeRecordList.get(i).getId().getExchangeTime()));
		//userInfo  = userInfoService.getUserInfo("1");
		rule = exchangeManagementService.getRule(1);
		giftList = exchangeManagementService.getAllGift();
		return SUCCESS;
	}
	public String exchangeRecordManage()
	{
		if(message != null)
		{
			ActionContext.getContext().put("message", message);
			message = null;
		}
		exchangeRecordList = exchangeService.getAllExchangeRecord();
		for(int i=0;i<exchangeRecordList.size();i++)
			dateList.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(exchangeRecordList.get(i).getId().getExchangeTime()));
		return SUCCESS;
	}
	public String changeExchangeRecordStatus()
	{
		try{
			exchangeRecord = exchangeService.getExchangeRecord(exchangeRecord.getId());
			exchangeRecord.setExchangeStatus("是");
			if(exchangeService.updateExchangeRecord(exchangeRecord))
				message = "修改成功";
			else
			{
				message = "修改失败";
				return ERROR;
			}
			return SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
			message = "修改失败";
			return ERROR;
		}
		
	}
}
